package com.wondersgroup.fjzlpt.org.controller.view.jgqy.fwgl;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.quick.framework.util.QuickPrincipalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.web.utils.RequestUtils;
import com.quick.framework.boot.web.wapper.QuickPrincipal;
import com.wondersgroup.fjzlpt.org.dao.inter.fwxx.FwxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.blxx.BlxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.fwgl.FwglYljgDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.fwgl.LyjlDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.fwgl.TljlDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.qysq.QysqDAO;
import com.wondersgroup.fjzlpt.org.service.inter.jgqy.fwgl.FwglService;

@Controller
@RequestMapping("fwgl")
public class FwglController {
	
	@Autowired
	private QysqDAO qysqDAO;
	
	@Autowired
	private FwglService fwglService;
	
	@Autowired
	private FwxxDAO fwxxDAO;
	
	@Autowired
	private FwglYljgDAO fwglYljgDAO;
	
	@Autowired
	private BlxxDAO blxxDAO;
	
	@Autowired
	private TljlDAO tljlDAO;
	
	@Autowired
	private LyjlDAO lyjlDAO;
	
	@RequestMapping("/index.html")
	public String index(HttpServletRequest request)
	{
        JSONObject json = new JSONObject();
        json.put("PTJGID", QuickPrincipalUtils.getPtjgID(request));
        json.put("WSJGLBDM", QuickPrincipalUtils.getJglbdm(request));
        request.setAttribute("userInfo", json);
		return "jgqy/fwgl/index";
	}
	
	@RequestMapping("/manager.html")
	public String manager(HttpServletRequest request)
	{
		
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		/**判断从新增还是修改方式进入*/
		if(map.containsKey("FWGLID")){
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject json = fwglService.queryPage(map);
			json = json.getJSONArray("rows").getJSONObject(0);
			JSONArray ja = fwglYljgDAO.query(map);
			Iterator it = ja.iterator();
			String jsfwjg = "";
			while(it.hasNext()){
				JSONObject jg = JSONObject.parseObject(it.next().toString());
				jsfwjg+= jg.getString("YLJGID")+",";
			}
			json.put("JSFWJG", jsfwjg.substring(0, jsfwjg.lastIndexOf(",")));
			request.setAttribute("fwglxx",json);
			/**接收服务机构*/
			map.put("YLTID", json.getString("YLTID"));
			ja = qysqDAO.queryQyJg(map);
			request.setAttribute("jsjglb", ja);
			/**获取医联体信息*/
			map.put("YLTID", json.getString("YLTID"));
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject jo = qysqDAO.queryYltxx(map);
			jo = (JSONObject) (jo.getJSONArray("rows")).get(0);
			request.setAttribute("qysqxx", jo);
			
	        JSONArray fwxxArr = fwxxDAO.queryXyfwxx(map);
	        request.setAttribute("fwxx", fwxxArr);
		}
		/**设置提供服务机构信息*/
        JSONObject jg = new JSONObject();
        jg.put("YLJGID", QuickPrincipalUtils.getPtjgID(request));
        jg.put("YLJGMC", QuickPrincipalUtils.getUserinfoByKey(request , "YLJGMC"));
        request.setAttribute("tgfwjgxx", jg);
		return "jgqy/fwgl/manager";
	}
	
	@RequestMapping("/detail.html")
	public String detail(HttpServletRequest request)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		if(map.containsKey("FWGLID")){
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject json = fwglService.queryPage(map);
			json = (JSONObject) (json.getJSONArray("rows")).get(0);
			JSONArray ja = fwglYljgDAO.query(map);
			Iterator it = ja.iterator();
			String jsfwjg = "";
			while(it.hasNext()){
				JSONObject jg = JSONObject.parseObject(it.next().toString());
				jsfwjg+= jg.getString("YLJGMC")+",";
			}
			json.put("JSFWJG", jsfwjg.substring(0, jsfwjg.lastIndexOf(",")));
			request.setAttribute("fwglxx",json);
			/**获取医联体信息*/
			map.put("YLTID", json.getString("YLTID"));
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject jo = qysqDAO.queryYltxx(map);
			jo = (JSONObject) (jo.getJSONArray("rows")).get(0);
			request.setAttribute("qysqxx", jo);
			/**设置提供服务机构信息*/
			/*QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
			JSONObject userInfo = principal.getUserInfo();
			JSONObject jg = new JSONObject();
			jg.put("YLJGID", userInfo.getString("PTJGID"));
			jg.put("YLJGMC", userInfo.getString("YLJGMC"));
			request.setAttribute("tgfwjgxx", jg);*/
			/**履约信息*/
			JSONObject lyjl = lyjlDAO.query(map);
			request.setAttribute("lyjl", lyjl);
		}
		return "jgqy/fwgl/detail";
	}
	
	@RequestMapping("/ylt.html")
	public String ylt(HttpServletRequest request)
	{
		return "jgqy/fwgl/ylt";
	}
	
	@RequestMapping("/bl.html")
	public String bl(HttpServletRequest request)
	{
		return "jgqy/fwgl/bl";
	}
	
	@RequestMapping("/lvyue.html")
	public String lvyue(HttpServletRequest request)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		/**判断从新增还是修改方式进入*/
		if(map.containsKey("FWGLID")){
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject json = fwglService.queryPage(map);
			json = (JSONObject) (json.getJSONArray("rows")).get(0);
			JSONArray ja = fwglYljgDAO.query(map);
			Iterator it = ja.iterator();
			String jsfwjg = "";
			while(it.hasNext()){
				JSONObject jg = JSONObject.parseObject(it.next().toString());
				jsfwjg+= jg.getString("YLJGMC")+",";
			}
			json.put("JSFWJG", jsfwjg.substring(0, jsfwjg.lastIndexOf(",")));
			request.setAttribute("fwglxx",json);
			/**获取医联体信息*/
			map.put("YLTID", json.getString("YLTID"));
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject jo = qysqDAO.queryYltxx(map);
			jo = (JSONObject) (jo.getJSONArray("rows")).get(0);
			request.setAttribute("qysqxx", jo);
			/**设置提供服务机构信息*/
			QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
			JSONObject userInfo = principal.getUserInfo();
			JSONObject jg = new JSONObject();
			/*jg.put("YLJGID", userInfo.getString("PTJGID"));
			jg.put("YLJGMC", userInfo.getString("YLJGMC"));*/
			jg.put("XM", QuickPrincipalUtils.getUserinfoByKey(request , "XM"));
			jg.put("USERID", QuickPrincipalUtils.getUserID(request));
			request.setAttribute("userInfo", jg);
			
		}
		return "jgqy/fwgl/fwly";
	}
	
	@RequestMapping("/talk.html")
	public String talk(HttpServletRequest request)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		/**判断从新增还是修改方式进入*/
		if(map.containsKey("FWGLID")){
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject json = fwglService.queryPage(map);
			json = (JSONObject) (json.getJSONArray("rows")).get(0);
			JSONArray ja = fwglYljgDAO.query(map);
			Iterator it = ja.iterator();
			String jsfwjg = "";
			while(it.hasNext()){
				JSONObject jg = JSONObject.parseObject(it.next().toString());
				jsfwjg+= jg.getString("YLJGMC")+",";
			}
			json.put("JSFWJG", jsfwjg.substring(0, jsfwjg.lastIndexOf(",")));
			request.setAttribute("fwglxx",json);
			/**获取医联体信息*/
			map.put("YLTID", json.getString("YLTID"));
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject jo = qysqDAO.queryYltxx(map);
			jo = (JSONObject) (jo.getJSONArray("rows")).get(0);
			request.setAttribute("qysqxx", jo);
			/**设置提供服务机构信息*/
			QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
			JSONObject userInfo = principal.getUserInfo();
			JSONObject jg = new JSONObject();
			jg.put("YLJGID", userInfo.getString("PTJGID"));
			jg.put("YLJGMC", userInfo.getString("YLJGMC"));
			jg.put("XM", userInfo.getString("XM"));
			jg.put("USERID", userInfo.getString("USERID"));
			request.setAttribute("userInfo", jg);
			JSONArray tljl = tljlDAO.query(map);
			request.setAttribute("tljl", tljl);
		}
		return "jgqy/fwgl/fwtl";
	}
	
	@RequestMapping("/blDetail.html")
	public String blDetail(HttpServletRequest request)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		map.put("pageSize", "1");
		map.put("pageNumber", "1");
		JSONObject jo = blxxDAO.queryPage(map);
		jo = (JSONObject) (jo.getJSONArray("rows")).get(0);
		request.setAttribute("blxx", jo);
		return "jgqy/fwgl/blDetail";
	}
	
	@RequestMapping("/fwlb.html")
	public String fwlb(HttpServletRequest request)
	{
		QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
        JSONObject userInfo = principal.getUserInfo();
        JSONObject json = new JSONObject();
        json.put("PTJGID", userInfo.get("PTJGID"));
        json.put("JGXZQHDM", userInfo.get("JGXZQHDM"));
        request.setAttribute("userInfo", json);
		return "jgqy/fwgl/fwlb";
	}
	
	@RequestMapping("/fwlbDetail.html")
	public String fwlbDetail(HttpServletRequest request)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		if(map.containsKey("FWGLID")){
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject json = fwglService.queryPage(map);
			json = (JSONObject) (json.getJSONArray("rows")).get(0);
			JSONArray ja = fwglYljgDAO.query(map);
			Iterator it = ja.iterator();
			String jsfwjg = "";
			while(it.hasNext()){
				JSONObject jg = JSONObject.parseObject(it.next().toString());
				jsfwjg+= jg.getString("YLJGMC")+",";
			}
			json.put("JSFWJG", jsfwjg.substring(0, jsfwjg.lastIndexOf(",")));
			request.setAttribute("fwglxx",json);
			/**获取医联体信息*/
			map.put("YLTID", json.getString("YLTID"));
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject jo = qysqDAO.queryYltxx(map);
			jo = (JSONObject) (jo.getJSONArray("rows")).get(0);
			request.setAttribute("qysqxx", jo);
			/**设置提供服务机构信息*/
			/*QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
			JSONObject userInfo = principal.getUserInfo();
			JSONObject jg = new JSONObject();
			jg.put("YLJGID", userInfo.getString("PTJGID"));
			jg.put("YLJGMC", userInfo.getString("YLJGMC"));
			request.setAttribute("tgfwjgxx", jg);*/
			/**履约信息*/
			JSONObject lyjl = lyjlDAO.query(map);
			request.setAttribute("lyjl", lyjl);
		}
		return "jgqy/fwgl/fwlbDetail";
	}
}
